/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events;


import java.util.Iterator;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.GUIDStorage;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.player.SimplePlayer;

/**
 * @author Massimo Canonaco
 *
 */
public class StartingPositionEvent implements GenericEvent {

	/** */
	static public final float GUID = GUIDStorage.STARTING_POSITION_EVENT;
	
	/** */
	private Character subject;
	
	/** */
	private SimplePlayer performer;
	
	/** */
	private int rowCell;
	
	/** */
	private int columnCell;
	
	/** */
	private boolean add;
	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.GenericEvent#getGUID()
	 */
	@Override
	public float getGUID() 
	{
		return GUID;
	}

	/* (non-Javadoc)
	 * @see it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.events.GenericEvent#getName()
	 */
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @return the subject
	 */
	public Character getSubject() 
	{
		return subject;
	}

	/**
	 * @param subject the subject to set
	 */
	public void setSubject(Character subject) {
		this.subject = subject;
	}

	/**
	 * @return the add
	 */
	public boolean isAdd() {
		return add;
	}

	/**
	 * @param add the add to set
	 */
	public void setAdd(boolean add) {
		this.add = add;
	}

	/**
	 * @return the point
	 */
	public int getRowCell() {
		return rowCell;
	}

	/** */
	public int getColumnCell()
	{
		return columnCell;
	}
	/**
	 * @param point the point to set
	 */
	public void setPoint(int row, int column) {
		rowCell = row;
		columnCell = column;
	}

	/**
	 * @return the performer
	 */
	public SimplePlayer getPerformer() {
		return performer;
	}

	/**
	 * @param performer the performer to set
	 */
	public void setPerformer(SimplePlayer performer) {
		this.performer = performer;
	}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject getAsJSONObject() 
	{
		
		JSONObject object = new JSONObject();
		object.put(GLOBAL_GUID_KEY, GUID);
		
		object.put(PERFORMER_NICK_KEY, performer.getNickname());
		object.put(CHARACTER_NAME_KEY, subject.getName());
		object.put(ADD_KEY, add);
		JSONArray array = new JSONArray();
		array.add(rowCell);
		array.add(columnCell);
		
		object.put(POSITION_KEY_KEY, array);
		return object;
	}

	@Override
	public void setFromJSONOnbject(JSONObject object) 
	{
		subject = new Character(0, 0, null);
		performer = new SimplePlayer();
		performer.setNickname((String)object.get(PERFORMER_NICK_KEY));
		subject.setName((String)object.get(CHARACTER_NAME_KEY));
		add = (Boolean)object.get(ADD_KEY);
		Iterator<Long> iterator = ((JSONArray)object.get(POSITION_KEY_KEY)).iterator();
		rowCell = iterator.next().intValue();
		columnCell = iterator.next().intValue();
//		subject.setPosition(iterator.next(), iterator.next());
	}

	/** */
	static private final String PERFORMER_NICK_KEY = "who";
	static private final String CHARACTER_NAME_KEY = "thisone";
	static private final String POSITION_KEY_KEY = "startsAt";
	static private final String ADD_KEY = "isAdd";
}
